package com.yygo.webapp.controller.material;

import com.yygo.Constants;
import com.yygo.model.*;
import com.yygo.model.enums.MaterialStatus;
import com.yygo.model.enums.Phase;
import com.yygo.service.LoanProductManager;
import com.yygo.service.LoanRequestManager;
import com.yygo.service.MaterialAuditManager;
import com.yygo.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/10/31.
 */
@Controller
@RequestMapping("/submitMaterials*")
public class SubmitMaterialsFormController extends BaseMaterialController {

    @Autowired
    private MaterialAuditManager materialAuditManager;

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Autowired
    private UserManager userManager;

    @RequestMapping(method = RequestMethod.GET)
    public String batchReceive(@RequestParam(required = true, value = "maId") Long maId,
                              Principal principal) {
        String returnView = "";
        User loginUser = userManager.getUserByUsername(principal.getName());
        MaterialAudit materialAudit = materialAuditManager.get(maId);
        if (materialAudit.getMaterialStatus().equals(MaterialStatus.Receiving)) {
            if (!loginUser.hasRole(Constants.Material_Receive_ROLE)) {
                throw new AccessDeniedException("You have no right to receive attachments");
            }
            materialAudit.setMaterialStatus(MaterialStatus.Auditing);
            materialAuditManager.save(materialAudit);
            returnView = "redirect:/materialReceiveList";
        }
        else if (materialAudit.getMaterialStatus().equals(MaterialStatus.Auditing)){
            if (!loginUser.hasRole(Constants.Material_Audit_ROLE)) {
                throw new AccessDeniedException("You have no right to audit attachments");
            }
            materialAudit.setMaterialStatus(MaterialStatus.Printing);
            materialAuditManager.save(materialAudit);
            returnView = "redirect:/materialAuditList";
        }
        else if (materialAudit.getMaterialStatus().equals(MaterialStatus.Printing)){
            if (!loginUser.hasRole(Constants.Material_Print_ROLE)) {
                throw new AccessDeniedException("You have no right to print attachments");
            }
            materialAudit.setMaterialStatus(MaterialStatus.Complete);
            materialAuditManager.save(materialAudit);
            LoanRequest loanRequest = loanRequestManager.loadWholeLoanRequest(materialAudit.getLoanRequest().getId());
            loanRequest.setPhase(Phase.bank_loan_audit);
            loanRequestManager.saveLoanRequest(loanRequest);
            returnView = "redirect:/materialPrintList";
        }
        return returnView;
    }
}
